package server

import (
	"berry-tv/media"
	"berry-tv/storage"
	"fmt"
	"github.com/gofiber/fiber/v2"
	"log"
	"time"
)

var db storage.DB

func Run(addr string, port int, dbPath string, mediaRoots []string) {
	log.Println("database:", dbPath)
	db, _ = storage.Open(dbPath)
	media.InitDB(db, mediaRoots)

	log.Println("addr:", addr)
	log.Println("port:", port)

	app := fiber.New()

	app.Get("/ping", func(c *fiber.Ctx) error {
		return c.SendString("pong " + time.Now().Format("2006-01-02 15:04:05"))
	})

	app.Static("/video", "./www/video", fiber.Static{
		ByteRange: true,  // enable byte-range request support
		Browse:    false, //
		MaxAge:    3600,  // browser can cache responses for 1 hour, reducing the number of requests to the server and improving performance
	})

	app.Static("/", "./www/")

	app.Listen(addr + ":" + fmt.Sprintf("%d", port))
}
